﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using Volo.Abp.Uow;

namespace Himp.Platform
{
    public class DataModelPropertyDictManager: DomainService
    {
        private readonly IRepository<StdWordDict, Guid> _stdWordDictRepository;
        private readonly IRepository<StdDomainDict, Guid> _stdDomainDictRepository;
        private readonly IRepository<StdPhrDict, Guid> _stdPhrDictRepository;
        private readonly IRepository<DataModelPropertyDict, Guid> _dataModelPropertyDictRepository;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public DataModelPropertyDictManager(IRepository<StdWordDict, Guid> stdWordDictRepository
            , IRepository<StdDomainDict, Guid> stdDomainDictRepository
            , IRepository<DataModelPropertyDict, Guid> dataModelPropertyDictRepository
            , IUnitOfWorkManager unitOfWorkManager
            , IRepository<StdPhrDict, Guid> stdPhrDictRepository)
        {
            _stdWordDictRepository = stdWordDictRepository;
            _stdDomainDictRepository = stdDomainDictRepository;
            _dataModelPropertyDictRepository = dataModelPropertyDictRepository;
            _unitOfWorkManager = unitOfWorkManager;
            _stdPhrDictRepository = stdPhrDictRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<DataModelPropertyDict> Create(Guid dataModelDictId, Guid? stdPhrDictId
            , string phrCode, string phrName, EnumLogicalDataType logicalDataType
            , int dataLen, int dataScale, bool pkFlag, bool nullableFlag, int srtno, string? exp)
        {
            if (stdPhrDictId == null)
            {
                string[] words = Regex.Split(phrCode, @"(?<!^)(?=[A-Z])");

                for (int i = 0; i < words.Length; i++)
                {
                    var c = await _stdWordDictRepository.CountAsync(s => s.EnAbbr == words[i]);

                    if (c == 0)
                    {
                        StdWordDict stdWordDict = new StdWordDict(words[i], phrName, words[i]);
                        await _stdWordDictRepository.InsertAsync(stdWordDict, autoSave: true);
                    }

                    if (i == words.Length - 1)
                    {
                        var stdDomainDict = await _stdDomainDictRepository.FirstOrDefaultAsync(s => s.DomainCodg == words[i]);

                        if (stdDomainDict == null)
                        {
                            stdDomainDict = new StdDomainDict(words[i], phrName, logicalDataType
                                , dataLen, dataScale, exp);
                            stdDomainDict = await _stdDomainDictRepository.InsertAsync(stdDomainDict, autoSave: true);
                        }

                        var ccc = await _stdPhrDictRepository.CountAsync(s => s.PhrCode == phrCode);

                        if (ccc == 0)
                        {
                            StdPhrDict stdPhrDict = new StdPhrDict(phrCode, phrName, stdDomainDict.Id, exp);
                            stdPhrDict = await _stdPhrDictRepository.InsertAsync(stdPhrDict);
                            stdPhrDictId = stdPhrDict.Id;
                        }
                    }
                }

                //var uow = _unitOfWorkManager.Current;

                //if (uow != null)
                //{ 
                //    await uow.SaveChangesAsync();
                //}
            }

            DataModelPropertyDict model = new DataModelPropertyDict(dataModelDictId, stdPhrDictId.Value
                    , pkFlag, nullableFlag, srtno);
            model = await _dataModelPropertyDictRepository.InsertAsync(model);

            return model;
        }
    }
}
